﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DataTransfer;
using Display;
using EnglishLibrary;
using EnglishReminder.Functions;
using EnglishReminder.Options;
using EnglishReminder.Properties;
using Global;
using Settings = Global.Settings;
using Helper;
using System.Drawing;

namespace EnglishReminder
{
    public partial class MainConfigurationForm : Form
    {
        #region Private Attributes
        private TestReminder trf;
        private CategoryDT cateDT = new CategoryDT();
        private const string STATUS_HEADER = @"Current Profile is: ";
        private const string STATUS_NO_FILE = @"No profile is chosen!!!";
        #endregion

        #region Constructors and Init methods
        public MainConfigurationForm()
        {
            InitializeComponent();
            RegistryHelper.InitTheRegistry();
            InitTheDialogs();
            InitTheMenuState();
        }
        private void MainConfigurationForm_Load(object sender, EventArgs e)
        {
            SetupTheForm();
        }
        private void InitTheMenuState()
        {
            if (string.IsNullOrEmpty(Settings.CURRENT_FILE_NAME))
            {
                DisableAllMenuItems();
            }
        }
        private void InitTheDialogs()
        {
            this.createFileDialog1.Filter = Constant.FILE_FILTERS;
            this.createFileDialog1.Title = Resources.CreateNewFileTitle;
            this.openFileDialog1.Filter = Constant.FILE_FILTERS;
            this.openFileDialog1.Title = Resources.OpenFileTitle;
        }
        private void SetupTheForm()
        {
            //Setup
            if (!string.IsNullOrEmpty(Settings.CURRENT_FILE_PATH) 
                && !string.IsNullOrEmpty(Settings.CURRENT_FILE_NAME)
                && File.Exists(Settings.CURRENT_FILE_PATH))
            {
                UpdateStatusMenuText(STATUS_HEADER + Settings.CURRENT_FILE_NAME);
                SetupMenuItems();
                ActivateAllMenuItems();
                SetupCategoryList();
            }
            else
            {
                DisableAllMenuItems();
                UpdateStatusMenuText(STATUS_NO_FILE);
            }
        }
        #endregion

        #region Events handling
        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (trf == null)
            {
                trf = new TestReminder();
                if (trf.IsNoItem == true)
                {
                    trf.Dispose();
                    trf = null;
                    MessageBox.Show(Resources.Warning_NoItem);
                    return;
                }

                trf.Show();
                startToolStripMenuItem.Enabled = false;
                stopToolStripMenuItem.Enabled = true;
                WindowState = FormWindowState.Minimized;
            }
        }
        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (trf != null)
            {
                trf.Dispose();
                trf = null;
                startToolStripMenuItem.Enabled = true;
                stopToolStripMenuItem.Enabled = false;
            }
        }
        private void wordToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            var aev = new AddEditVocabulary();
            aev.Show();
        }
        private void noteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var aen = new AddEditNote();
            aen.Show();
        }
        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var sf = new SettingsForm();
            sf.restartReminderForm = new SettingsForm.RestartReminderForm(RestartTheReminderForm);
            sf.Show();
        }
        private void filterTheResultsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var filter = new Filters();
            filter.Show();
        }
        private void createCategoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var cc = new CreateCategory();
            cc.Show();
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }
        private void MainConfigurationForm_Resize(object sender, EventArgs e)
        {
            // If the form's state is"Minimize", the hide the form
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dialogResult = this.createFileDialog1.ShowDialog();
            switch (dialogResult)
            {
                case DialogResult.Cancel:
                    break;
                case DialogResult.OK:
                    MainFormHelper.UpdateFilePathAndName(this.createFileDialog1.FileName);
                    MainFormHelper.SaveFilePathAndNameToRegistry();

                    //Create file
                    FileHelper.CreateNewProfile(Settings.CURRENT_FILE_PATH);
                    this.SetupTheForm();
                    break;
            }
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = string.Empty;
            var dialogResult = this.openFileDialog1.ShowDialog();

            switch (dialogResult)
            {
                case DialogResult.Cancel:
                    break;
                case DialogResult.OK:
                    //TODO: Need validate file before open

                    MainFormHelper.UpdateFilePathAndName(this.openFileDialog1.FileName);
                    MainFormHelper.SaveFilePathAndNameToRegistry();
                    this.SetupTheForm();
                    break;
            }
        }
        #endregion

        #region Private Utility Methods
        private void SetupMenuItems()
        {
            startToolStripMenuItem.Enabled = true;
            stopToolStripMenuItem.Enabled = false;
        }
        private void SetupCategoryList()
        {
            //don't save it in the registry
            Settings.CATEGORY_LIST_NO_DISPLAY = new Dictionary<int, string>();
            Settings.CATEGORY_LIST_DISPLAY = new Dictionary<int, string>();
            var cateList = new List<Category>();

            try
            {
                cateList = cateDT.GetListCategories();
            }
            catch (Exception ex)
            {
                DisableAllMenuItems();
                UpdateStatusMenuText(STATUS_NO_FILE);
            }

            foreach (var cate in cateList)
            {
                Settings.CATEGORY_LIST_NO_DISPLAY.Add(cate.CategoryID, cate.Name);
            }
        }
        private void DisableAllMenuItems()
        {
            this.startToolStripMenuItem.Enabled = false;
            this.stopToolStripMenuItem.Enabled = false;
            this.addToolStripMenuItem.Enabled = false;
            this.editItemsToolStripMenuItem.Enabled = false;
            this.createCategoryToolStripMenuItem.Enabled = false;
            this.settingsToolStripMenuItem.Enabled = false;
            this.filterTheResultsToolStripMenuItem.Enabled = false;
        }
        private void ActivateAllMenuItems()
        {
            this.addToolStripMenuItem.Enabled = true;
            this.editItemsToolStripMenuItem.Enabled = true;
            this.createCategoryToolStripMenuItem.Enabled = true;
            this.settingsToolStripMenuItem.Enabled = true;
            this.filterTheResultsToolStripMenuItem.Enabled = true;
        }
        private void UpdateStatusMenuText(string text)
        {
            this.toolStripStatusLabel1.Text = text;
        }
        #endregion

        #region Public Utility Methods
        public void RestartTheReminderForm()
        {
            if (trf != null)
            {
                //delete it
                trf.Dispose();
                trf = null;

                //restart it
                trf = new TestReminder();
                if (trf.IsNoItem)
                {
                    trf.Dispose();
                    trf = null;
                    return;
                }
                
                trf.Show();
                startToolStripMenuItem.Enabled = false;
                stopToolStripMenuItem.Enabled = true;
            }
        }
        #endregion

        private void quickMergeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MergeProfile mf = new MergeProfile();
            mf.Show();
        }

        private void advancedMergeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
